sqlite: add batched async Database API#62015
sqlite: add batched async Database API#62015BurningEnlightenment wants to merge 22 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
|
@geeksilva97 given that you spearheaded the previous attempt, what are your thoughts regarding this approach? |
The `node:sqlite` module `Initialize` would get quite large if both of the async and sync database templates were embedded. Therefore move the template creation into a seperate function. I've avoided the `GetConstructorTemplate` pattern, because it seems to imply exposing the template via `PER_ISOLATE_TEMPLATE_PROPERTIES` which is unnecessary in our case.
Previously all `SQLTagStore` instances had unique prototypes. Note that the class and its prototype are currently not exposed on `node:sqlite`, i.e. it currently can't be directly used for `instanceOf` checks.
The database opening and configuration logic can be shared between the sync and async API variants, therefore extract the shared implementation into a common base class.
Add a Database class skeleton which is currently only capable of opening and closing a sqlite db connection. Also notably missing is any support for asynchronous operations apart from changes to the close and dispose method signatures. However, the skeleton is capable enough to pass the most basic lifetime tests and therefore useful as a diff target for the asynchronous operations to be added. Re-enable the async database test suites and skip the tests on a case-by -case basis.
Trade a bit of db operation latency for throughput by batching db operations together before scheduling their execution on the thead pool. Completion notifications are currently delivered eagerly through an additional `uv_async` handle, but this is not strictly necessary for the chosen implementation strategy and might get removed again. Some care has been taken to not criss-cross memory allocations and de- allocations between threads as modern modern allocators tend to perform worse in such scenarios.
The linter doesn't seem to like these. Is it a linter bug?
ad96130 to
1eceaf7
Compare
Async methods should always asynchronously reject with an error instead of throwing a synchronous error. Therefore convert the previous invalid state synchronous throws to asynchronous rejections for the Database and Statement classes.
Async methods should always asynchronously reject with an error instead of throwing a synchronous error. Therefore convert the previous invalid args synchronous throws to asynchronous rejections for the Database and Statement classes.
|
@anonrig given your offer in #57445 (comment), can you give this a preliminary review? |
Respect `readBigInts` and `returnArrays` options when executing prepared statements.
|
Is there a way to obtain some additional information for the test failure in https://github.com/nodejs/node/actions/runs/22662548422/job/65687807294#step:7:6087 ? Locally I'm using the devcontainer and |
The macos job https://github.com/nodejs/node/actions/runs/22662548422/job/65687807238#step:7:26344 indicates that sqlite is not build in the shared library variant. So probably a case of ESM and |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62015 +/- ##
==========================================
- Coverage 89.64% 89.59% -0.05%
==========================================
Files 676 676
Lines 206249 207339 +1090
Branches 39518 39699 +181
==========================================
+ Hits 184892 185771 +879
- Misses 13479 13607 +128
- Partials 7878 7961 +83
🚀 New features to boost your workflow:
|
This PR adds the asynchronous complements
DatabaseandStatementto thenode:sqlitemodule.Given that a single sqlite db connection cannot scale beyond a single CPU core, (unlike the old
sqlite3) this implementation avoids unordered dispatches to the thread pool by maintaining a strict FIFO operation queue. This allows usage of multiple db connections in parallel without relying on the internal db connection mutex (=>SQLITE_THREADSAFE=2is sufficient). Furthermore I've favored throughput over latency, e.g. it collects asynchronous operations on a db connection and dispatches them in batches to the thread pool. This helps to amortize the inherent synchronization overhead over multiple operations and it also improves cache locality.The implementation effort has reached a maturity level where the overall approach should only require relatively small incremental changes to reach the outlined API scope. Briefly the following things work:
Database.prototype.exec()without a prepared statement.Statement.prototype.get()|all()|run()with binding parameters (though currently not as fancy as the sync variant).Things which still need work:
Database#location(dbName?: string): Promise<string|null>Database#loadExtension(path: string): Promise<void>;Database#enableLoadExtension(allow: boolean): Promise<void>Database#enableDefensive(active: boolean): Promise<void>std::pmr::monotonic_buffer_resourcein a few places.v8::Promiselooses the async context/callstack. I need some guidance on how to fix that.Symbol.dispose/using-based (different PR?)DatabasetoDatabaseSyncand vice versa (different PR?)Fixes #54307
Previous attempt #59109 (I've only taken the test suites from there).
API synopsis
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Footnotes
I've followed the spirit of
DEP0137; should probably be reconciled with the sync API at some point. ↩